﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EncuentraloSilverlight.ServiceReference2;
using System.Windows.Navigation;
using System.Windows.Browser;
using System.IO;
using System.Windows.Media.Imaging;
using System.Xml;
using System.Xml.Serialization;

namespace EncuentraloSilverlight
{
    public partial class AddProduct : Page
    {
        public AddProduct()
        {
            // Required to initialize variables
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Home.xaml", UriKind.Relative));
        }

        private void TextBox_CommonExpressSearch_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void Agregar_Producto_A_bd(object sender, RoutedEventArgs e)
        {
            int precio = -1;
            if (textboxNombreProducto.Text.Length != 0 && textboxPrecioProducto.Text.Length != 0
                && textboxDescripcionProducto.Text.Length != 0 && int.TryParse(textboxPrecioProducto.Text, out precio))
            {
                Service2Client webservice = new Service2Client();

                webservice.FindSellerFromNameCompleted += (obj, eventarg) =>
                    {


                        Product producto = new Product()
                        {
                            Name = textboxNombreProducto.Text,

                            Price = precio,

                            Description = textboxDescripcionProducto.Text,
                        };

                        if (image_bytes != null)
                        {                            
                            producto.Image = new Binary() { Bytes=image_bytes };
                        }


                        producto.SellerID = eventarg.Result[0].SellerID;
                        

                        webservice.AddProductCompleted += (objj, eventargg) =>
                        {
                            MessageBox.Show("Producto Agregado con exito");
                            NavigationService.Navigate(new Uri("/Views/Seller_View.xaml", UriKind.Relative));
                        };

                        webservice.AddProductAsync(producto);
                    };

                webservice.FindSellerFromNameAsync(Cookie.GetCookie("EncuentraloUser"));

            }
        }


        byte[] image_bytes = null;
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            if (dlg.ShowDialog().Value)
            {
                BitmapImage imageSource = new BitmapImage();
                Stream stream = dlg.File.OpenRead();
                BinaryReader reader = new BinaryReader(stream, System.Text.Encoding.Unicode);
                image_bytes = reader.ReadBytes((int)stream.Length);
                stream.Position = 0;
                imageSource.SetSource(stream);             
            }
        }


    }
}